package com.timi_licai.timi_licai_account;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.adapter.AddCategoryCustomAdapter;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.dialog.CategoryCustomDialog;
import com.timi_licai.timi_licai_account.dialog.UpdateCategoryCustomDialog;
import com.timi_licai.timi_licai_account.listener.CategoryCustomInterface;
import com.timi_licai.timi_licai_account.listener.CategoryUpdateInterface;
import com.timi_licai.timi_licai_account.model.ShowCategory;

import java.util.LinkedList;
import java.util.List;


public class AddCategoryActivity extends ActionBarActivity implements View.OnClickListener, CategoryCustomInterface, CategoryUpdateInterface {
    private ListView showCategoryListView;
    private List<ShowCategory> categoryListFromDB;
    private AddCategoryCustomAdapter addCategoryCustomAdapter;
    private Button addCategoryBtn;
    private CategoryCustomDialog addCategoryDialog;
    private String addCategoryDialogEdit;
    private UpdateCategoryCustomDialog updateCategoryDialog;
    private TextView addDone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_add_category);
        showCategoryListView = (ListView) findViewById(R.id.show_category_list_view);
        addCategoryBtn = (Button) findViewById(R.id.btn_add_category);
        addDone = (TextView) findViewById(R.id.add_category_done);
        addDone.setOnClickListener(this);
        addCategoryBtn.setOnClickListener(this);
        // 获取分类列表数据
        categoryListFromDB = getShowCategoryListFromDB();
        addCategoryCustomAdapter = new AddCategoryCustomAdapter(this, categoryListFromDB);
        showCategoryListView.setAdapter(addCategoryCustomAdapter);
    }


    /**
     * 从数据库中查出表中所有的分类的类别信息
     *
     * @return
     */
    private List<ShowCategory> getShowCategoryListFromDB() {
        List<ShowCategory> categoryList = new LinkedList<>();
        Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_CONSUME_TYPE, null, "ismark=?", new String[]{0 + ""}, null);
        ShowCategory showCategory;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                showCategory = new ShowCategory();
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String typename = cursor.getString(cursor.getColumnIndex("typename"));
                String typepath = cursor.getString(cursor.getColumnIndex("typepath"));
                showCategory.setId(id);
                showCategory.setTypeName(typename);
                showCategory.setTypePath(typepath);
                categoryList.add(showCategory);
            }
        }
        return categoryList;
    }

    public void deleteItem(int position) {
        ShowCategory showCategory = categoryListFromDB.get(position);
        int id = showCategory.getId();
        String selection = "_id=?";
        String[] selectionArgs = new String[]{id + ""};
        ContentValues values = new ContentValues();
        values.put("ismark", 1);
        DatabaseUtils.update(MyConstants.TABLE_CONSUME_TYPE, values, selection, selectionArgs);
        // int delete = DatabaseUtils.delete(MyConstants.TABLE_CONSUME_TYPE, selection, selectionArgs);
        List<ShowCategory> list = getShowCategoryListFromDB();
        categoryListFromDB.clear();
        categoryListFromDB.addAll(list);
        addCategoryCustomAdapter.notifyDataSetChanged();
    }

    public void updateItem(int position) {
        //点击了编辑按钮
        String typeName = categoryListFromDB.get(position).getTypeName();
        int id = categoryListFromDB.get(position).getId();
        updateCategoryDialog = new UpdateCategoryCustomDialog(this, R.style.addCategoryDialog, this, typeName, id);
        Window window = updateCategoryDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        updateCategoryDialog.show();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.add_category_done:
                this.finish();
                break;
            case R.id.btn_add_category:
                addCategoryDialog = new CategoryCustomDialog(this, R.style.addCategoryDialog, this);
                Window window = addCategoryDialog.getWindow();
                //todo  在数据库中插入数据 点击后弹出对话框
                window.setGravity(Gravity.CENTER);
                addCategoryDialog.show();
                break;
        }
    }

    @Override
    public void getCategoryCustomDialogClick(int id) {
        switch (id) {
            case R.id.dialog_category_custom_cancel:
                //点击了取消按钮
                addCategoryDialog.cancel();
                break;
            case R.id.dialog_category_custom_ensure:
                //点击了完成按钮
                addCategoryDialog.cancel();
                break;
        }
    }

    @Override
    public void getCategoryCustomDialogEdit(String str) {
        addCategoryDialogEdit = str;
        if (!TextUtils.isEmpty(str)) {
            ContentValues values = new ContentValues();
            values.put("typename", str);
            DatabaseUtils.insert(MyConstants.TABLE_CONSUME_TYPE, values);
        }
        List<ShowCategory> listFromDB = getShowCategoryListFromDB();
        categoryListFromDB.clear();
        categoryListFromDB.addAll(listFromDB);
        addCategoryCustomAdapter.notifyDataSetChanged();
    }

    /**
     * 修改分类的回调接口
     *
     * @param id
     */
    @Override
    public void updateCategoryClickId(int id) {
        switch (id) {
            case R.id.dialog_category_update_cancel:
                //点击了取消按钮
                updateCategoryDialog.cancel();
                break;
            case R.id.dialog_category_update_ensure:
                //点击了完成按钮
                updateCategoryDialog.cancel();
                break;
        }
    }

    @Override
    public void updateCategoryEditText(String str, int position) {
        addCategoryDialogEdit = str;
        if (!TextUtils.isEmpty(str)) {
            ContentValues values = new ContentValues();
            values.put("typename", str);
            DatabaseUtils.update(MyConstants.TABLE_CONSUME_TYPE, values, "_id=?", new String[]{position + ""});
        }
        List<ShowCategory> listFromDB = getShowCategoryListFromDB();
        categoryListFromDB.clear();
        categoryListFromDB.addAll(listFromDB);
        addCategoryCustomAdapter.notifyDataSetChanged();
    }
}
